package com.easyjava.builder;

import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BuildQuery
 * @Date 2024/6/18  10:47
 **/
public class BuildQuery {
    private static final Logger logger = LoggerFactory.getLogger(BuildPo.class);

    public static void execute(TableInfo tableInfo) {


        File folder = new File(Constants.PATH_QUERY);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        String className = tableInfo.getBeanName() + Constants.SUFFIX_BEAN_QUERY;
        File file = new File(folder, className + ".java");


        try {
            OutputStream outputStream = Files.newOutputStream(file.toPath());
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write("package " + Constants.PACKAGE_QUERY + ";");
            bufferedWriter.newLine();
            bufferedWriter.newLine();
            if (tableInfo.getHaveDate() || tableInfo.getHaveDateTime()) {
                bufferedWriter.write("import java.util.Date;");
                bufferedWriter.newLine();
            }
            if (tableInfo.getHavaBigDecimal()) {
                bufferedWriter.write("import java.math.BigDecimal;");
            }
            bufferedWriter.newLine();
            BuildComment.createClassComment(bufferedWriter, tableInfo.getComment() + "查询对象");
            bufferedWriter.write("public class " + className + " extends BaseQuery {");
            bufferedWriter.newLine();


            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {

                BuildComment.createFieldComment(bufferedWriter, fieldInfo.getComment());
                bufferedWriter.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";");
                bufferedWriter.newLine();
                if (ArrayUtils.contains(Constants.SQL_STRING_TYPE, fieldInfo.getSqlType())) {
                    String propertyName = fieldInfo.getPropertyName() + Constants.SUFFIX_QUERY_FUZZY;
                    bufferedWriter.write("\tprivate " + fieldInfo.getJavaType() + " " + propertyName + ";");
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();
                }


                if (ArrayUtils.contains(Constants.SQL_DATE_TYPE, fieldInfo.getSqlType()) || ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPE, fieldInfo.getSqlType())) {
                    String propertyNameStart = fieldInfo.getPropertyName() + Constants.SUFFIX_QUERY_TIME_START;
                    bufferedWriter.write("\tprivate String " + propertyNameStart + ";");
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();


                    String propertyNameEnd = fieldInfo.getPropertyName() + Constants.SUFFIX_QUERY_TIME_END;
                    bufferedWriter.write("\tprivate String " + propertyNameEnd + ";");
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();

                }
            }
            List<FieldInfo> fieldInfoList = tableInfo.getFieldList();
            fieldInfoList.addAll(tableInfo.getFieldExtendList());

            for (FieldInfo fieldInfo : fieldInfoList) {
                String tempField = StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName());
                bufferedWriter.write("\tpublic " + fieldInfo.getJavaType() + " get" + tempField + "() {");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\treturn " + fieldInfo.getPropertyName() + ";");
                bufferedWriter.newLine();
                bufferedWriter.write("\t}");
                bufferedWriter.newLine();


                bufferedWriter.write("\tpublic void set" + tempField + "(" + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ") {");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\tthis." + fieldInfo.getPropertyName() + " = " + fieldInfo.getPropertyName() + ";");
                bufferedWriter.newLine();
                bufferedWriter.write("\t}");
                bufferedWriter.newLine();
            }
            bufferedWriter.write("}");
            bufferedWriter.flush();
        } catch (Exception e) {
            logger.error("创建po失败", e);
        }
    }
}


